package com.cipri.battle.ui
{
	import com.cipri.battle.config.Settings;
    import com.cipri.battle.events.ShapeEvent;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
	
	import flash.display.Sprite;
	
	public class Shape extends Sprite
	{
		private var _id:uint;
		private var _definition:Array;
		private var _color:uint;
		private var _boxes:Array;
        
        private var _xStartDrag:Number;
        private var _yStartDrag:Number;
		
		public function Shape(id:uint)
		{
			_id = id;
			_definition = Settings["SHAPE_" + _id + "_DEFINITION"];
			
			if (Settings["SHAPE_" + _id + "_BACKGROUND_COLOR"]) 
			{
				_color = Settings["SHAPE_" + _id + "_BACKGROUND_COLOR"];
			}
			else 
			{
				_color = Settings["SHAPE_BACKGROUND_COLOR"];
			}
			
			drawShape();
            
			buttonMode = true;
			initEventListeners();
		}
        
        public function getTopLeftCenter():Point 
        {
            return localToGlobal(new Point(_boxes[0].width / 2, _boxes[0].height / 2));
        }
        
        public function resetPosition():void 
        {
            x = _xStartDrag;
            y = _yStartDrag;
        }
        
        public function disableMouseActions():void 
        {
            buttonMode = false;
            mouseEnabled = false;
            mouseChildren = false;
            removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        }
		
		private function initEventListeners():void 
		{
            addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
        
        private function rotate():void
        {
            var newDef:Array = new Array();
            
            for (var i:int = 0; i < _definition.length; i++) 
            {
                for (var j:int = 0; j < _definition[i].length; j++) 
                {
                    if (i == 0) 
                    {
                        newDef[j] = new Array();
                    }
                    newDef[j].push(_definition[i][j]);
                }
            }
            
            _definition = newDef;
            
            drawShape();
            
            dispatchEvent(new ShapeEvent(ShapeEvent.ROTATE));
        }
        
        private function drawShape():void 
		{
            var i:uint;
            var childCnt:uint = numChildren;
            for (i = 0; i < childCnt; i++) 
            {
                removeChildAt(0);
            }
			var box:Box;
            
            _boxes = new Array();
			
			for (i = 0; i < _definition.length; i++) 
			{
				for (var j:uint = 0; j < _definition[i].length; j++) 
				{
					if (_definition[i][j] != 0) 
					{
						box = new Box(_definition[i][j], _color);
						box.x = j * Settings.TABLE_CELL_SIZE;
						box.y = i * Settings.TABLE_CELL_SIZE;
						addChild(box);
						_boxes.push(box);
					}
				}
			}
		}		
        
        private function onMouseDown(event:MouseEvent):void 
        {
            _xStartDrag = x;
            _yStartDrag = y;
            alpha = Settings.SHAPE_DRAG_ALPHA;
            startDrag(false, new Rectangle(0, 0, Settings.WIDTH - width, Settings.HEIGHT - height));
            addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
        }
        
        private function onStageMouseUp(e:MouseEvent):void 
        {
            alpha = 1;
            stopDrag();
            resetPosition();
            removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
            removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
        
        private function onMouseUp(event:MouseEvent):void 
        {
            event.stopPropagation();
            stopDrag();
            alpha = 1;
            removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
            removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            
            if (x == _xStartDrag && y == _yStartDrag) 
            {
                //the user clicked the shape - we must rotate
                rotate();
            }
            else 
            {
                dispatchEvent(new ShapeEvent(ShapeEvent.DROP));
            }
        }
        
        private function onMouseMove(event:MouseEvent):void 
        {
            event.updateAfterEvent();
            //trace(localToGlobal(new Point(_boxes[0].width / 2, _boxes[0].height / 2)));
        }
        
        public function get definition():Array { return _definition; }
	}
}